
(function(){
    //渲染导航页
    fetch('http://chst.vip:1234/api/getbaicaijiatitle')
    .then(response => {
        return response.json()
    }).then(res => {
        let data = res.result
        console.log(data);
        let ele = ''
        data.forEach(item => {
            ele += `
                <li data-titleid="${item.titleId}">${item.title}</li>
            `
        });

        $('.navList').html(ele)

        $('.navList li').eq(0).addClass('.active')
        // 给导航的每个 li 绑定点击事件
        $('.navList li').click(function(){
            let index = $(this).attr('data-titleid')
            $(this).addClass('active').siblings('li').removeClass('active')

            // 每次点击 先清除 定时器
            clearInterval(timer)
            // 点击请求对应 导航 的 数据
            queryData(index)
        })
    });

    // 导航栏 滑动效果 tarch
    $('.navList').on('touchmove',function(e){

    })

    // 封装 渲染优惠券详情列表的函数
    function queryData(value){
        fetch('http://chst.vip:1234/api/getbaicaijiaproduct?titleid=' + value)
        .then(response => {
            return response.json()
        }).then(res => {
            let data = res.result
            console.log(data);
            let ele = ''
            data.forEach(item => {
                ele += `
                <li>
                    <a href="">
                        <div class="left">
                            ${item.productImg}
                        </div>
                        <div class="right">
                            <h4>
                                ${item.productName}
                            </h4>
                            <div class="price">
                                ${item.productPrice}<del>￥1888.0</del>
                            </div>
                            <div class="down">
                                <div class="bar">
                                    <div class="bar_in">0%</div>
                                </div>
                                <div class="yiling">
                                    已领<span class="a">0</span>张/剩余<span class="b">126</span>张
                                </div>
                                <div class="click">
                                    <div class="zuo">点此领取优惠券</div>
                                    <div class="you">下单链接</div>
                                </div>
                            </div>
                            
                        </div>
                    </a>
                 </li>
                `
            });
            $('#info').html(ele)
        })
    }

    queryData(0)


    let num = 0
    // 加载进度条
    let timer = setInterval(() => {
        // 调用随机数区间函数
        num += randomNum(1,3)
        $('.bar_in').css('width',num)

        let bar_inWidth = (parseInt($('.bar_in').css('width')))
        let barWidth = (parseInt($('.bar').css('width')))

        let value = bar_inWidth / barWidth * 100

        $('.bar_in').html(parseInt(value)+ '%')

        $('.yiling .a').html(num)
        $('.yiling .b').html(126 - num)

        if (bar_inWidth >= barWidth) {
            clearInterval(timer)
            bar_inWidth = barWidth
            $('.bar_in').html('100%')
            $('.yiling .a').html(126)
            $('.yiling .b').html(0)
        }

    }, 70);


})();